package com.Sdebruijn.curriculumvitae;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.Sdebruijn.curriculumvitae.db.DatabaseManager;
import com.Sdebruijn.curriculumvitae.fragments.MenuListFragment;
import com.Sdebruijn.curriculumvitae.fragments.PersonFragment;
import com.Sdebruijn.curriculumvitae.models.Applicant;
import com.Sdebruijn.curriculumvitae.swipey.BaseSwipeyTabsActivity;
import com.Sdebruijn.curriculumvitae.util.FragmentManagerUtils;
import com.Sdebruijn.curriculumvitae.util.OnClickUtils;
import com.actionbarsherlock.view.Menu;
import com.slidingmenu.lib.SlidingMenu;

/**
 * Base Activity for every Activity that contains SwipeyTabs. Sets a couple of
 * standard things like the ActionBar(Sherlock), SlidingMenu and ORMLite's
 * SQLite Database. Will also collect all onClick events of Fragments in the
 * PageViewer and in the SlidingMenu.
 * 
 * @author Stefan de Bruijn
 * 
 */
public class BaseActivity extends BaseSwipeyTabsActivity implements OnClickListener {

	private Fragment mFrag;
	private Applicant applicant;
	private FragmentManager fm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		DatabaseManager.init(this);
		fm = this.getSupportFragmentManager();

		getSupportActionBar().setDisplayUseLogoEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);
		getSupportActionBar().setTitle(R.string.app_name);
		getSupportActionBar().setDisplayShowHomeEnabled(true);
		getSupportActionBar().setLogo(R.drawable.ic_launcher);

		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
		mFrag = new MenuListFragment();
		t.replace(R.id.menu_frame, mFrag);
		t.commit();

		// Customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setShadowWidthRes(R.dimen.shadow_width);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindScrollScale(0.25f);
		sm.setFadeDegree(0.25f);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

	}

	public void onClick(View view) {
		switch (view.getId()) {
		// Menu items: buttons
		case R.id.menu_button_call:
			if (applicant == null) {
				applicant = (Applicant) DatabaseManager.getApplicant();
			}
			OnClickUtils.startPhoneCall(this, applicant.mobile);
			break;
		case R.id.menu_button_mail:
			if (applicant == null) {
				applicant = (Applicant) DatabaseManager.getApplicant();
			}
			OnClickUtils.sendMail(this, applicant.email);
			break;
		case R.id.menu_button_chat:
			Toast.makeText(
					this,
					"This feature is planned for version 1.1, You can see the complete roadmap in Google Project Hosting",
					Toast.LENGTH_LONG).show();
			// startActivity(new Intent(this,
			// ChatActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			break;
		case R.id.menu_button_add_contact:
			if (applicant == null) {
				applicant = (Applicant) DatabaseManager.getApplicant();
			}
			OnClickUtils.addAsContact(this, applicant);
			break;

		// Menu items: list items
		case R.id.menu_tv_cv:
			// TODO do this in a cleaner way.
			if (fm.findFragmentByTag("android:switcher:2130968702:1") instanceof PersonFragment) {
				this.toggle();
			} else {
				startActivity(new Intent(this, MainActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			}
			break;
		case R.id.menu_tv_android_projects:
			if (FragmentManagerUtils.getFieldmActivity(fm) instanceof AppsActivity) {
				this.toggle();
			} else {
				startActivity(new Intent(this, AppsActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			}
			break;
		case R.id.menu_tv_sidejobs:
			if (FragmentManagerUtils.getFieldmActivity(fm) instanceof SideJobsActivity) {
				this.toggle();
			} else {
				startActivity(new Intent(this, SideJobsActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			}
			break;
		case R.id.menu_tv_courses:
			if (FragmentManagerUtils.getFieldmActivity(fm) instanceof CourseActivity) {
				this.toggle();
			} else {
				startActivity(new Intent(this, CourseActivity.class));
			}
			break;
		case R.id.menu_tv_hobby:
			if (FragmentManagerUtils.getFieldmActivity(fm) instanceof HobbyActivity) {
				this.toggle();
			} else {
				startActivity(new Intent(this, HobbyActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			}
			break;
		case R.id.menu_tv_gph:
			if (applicant == null) {
				applicant = (Applicant) DatabaseManager.getApplicant();
			}
			OnClickUtils.openBrowser(this, applicant.online_person.google_project_hosting);
			break;
		case R.id.menu_tv_stackoverflow:
			if (applicant == null) {
				applicant = (Applicant) DatabaseManager.getApplicant();
			}
			OnClickUtils.openBrowser(this, applicant.online_person.stackoverflow);
			break;
		// Personal data: clickable fields
		case R.id.tv_email:
			TextView v = (TextView) view;
			OnClickUtils.sendMail(this, v.getText().toString());
			break;
		case R.id.tv_mobile:
			TextView vi = (TextView) view;
			OnClickUtils.startPhoneCall(this, vi.getText().toString());
			break;
		case R.id.rl_google_plus:
			if (applicant == null) {
				applicant = (Applicant) DatabaseManager.getApplicant();
			}
			OnClickUtils.openGooglePlusProfile(this, applicant.online_person.google_plus_id);
			break;

		case R.id.rl_linkedin:
			if (applicant == null) {
				applicant = (Applicant) DatabaseManager.getApplicant();
			}
			OnClickUtils.openLinkedinProfile(this, applicant.online_person.linkedin_id);
			break;
		}
	}

	@Override
	protected boolean addActionBarMenu(Menu menu, int position) {
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// Catch only the menu key. If anything else is pressed (E.g. the back
		// button), let the system handle it
		switch (keyCode) {
		case KeyEvent.KEYCODE_MENU:
			toggle();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
